const express = require('express');
const router = express.Router();

// 模拟新闻列表数据
const mockNewsData = [
  {
    id: 1,
    title: '转转双12大促来袭，全场商品限时特惠',
    content: '双12购物狂欢节即将开启，转转平台推出全场商品限时特惠活动，手机、数码、家电等热门品类均有惊喜折扣...',
    img: '/static/images/home/goods/1.png',
    create_time: '2023-12-10 09:30:00',
    read_count: 1250
  },
  {
    id: 2,
    title: '官方验机服务升级，新增10项检测标准',
    content: '为提升用户体验，转转官方验机服务进行全面升级，新增10项专业检测标准，确保每一台手机都经过严格质检...',
    img: '/static/images/home/goods/2.png',
    create_time: '2023-12-09 14:20:00',
    read_count: 890
  },
  {
    id: 3,
    title: '新用户专享福利，注册即送100元优惠券',
    content: '新用户注册转转平台即可获得100元优惠券大礼包，可在购买任意商品时使用，优惠力度空前...',
    img: '/static/images/home/goods/3.png',
    create_time: '2023-12-08 11:15:00',
    read_count: 1560
  },
  {
    id: 4,
    title: '转转直播带货专场，明星主播助阵',
    content: '本周五晚8点，转转直播带货专场邀请多位明星主播助阵，现场抽奖送iPhone15，不容错过...',
    img: '/static/images/home/goods/4.png',
    create_time: '2023-12-07 16:45:00',
    read_count: 2100
  },
  {
    id: 5,
    title: '平台规则更新通知，保障交易安全',
    content: '为保障用户交易安全，转转平台对交易规则进行优化更新，新增多项安全保障措施...',
    img: '/static/images/home/goods/5.png',
    create_time: '2023-12-06 10:00:00',
    read_count: 730
  }
];

// 获取新闻列表数据
router.post('/newsListData', (req, res) => {
  try {
    res.json({
      code: 200,
      message: 'success',
      data: mockNewsData
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: '服务器错误',
      data: null
    });
  }
});

// 获取新闻详情
router.post('/newsDetail', (req, res) => {
  try {
    const { id } = req.body;
    const newsDetail = mockNewsData.find(news => news.id === parseInt(id));
    
    if (!newsDetail) {
      return res.status(404).json({
        code: 404,
        message: '新闻不存在',
        data: null
      });
    }
    
    res.json({
      code: 200,
      message: 'success',
      data: newsDetail
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: '服务器错误',
      data: null
    });
  }
});

// 增加阅读量
router.post('/increaseReadCount', (req, res) => {
  try {
    const { id } = req.body;
    const news = mockNewsData.find(news => news.id === parseInt(id));
    
    if (news) {
      news.read_count += 1;
    }
    
    res.json({
      code: 200,
      message: 'success',
      data: { read_count: news ? news.read_count : 0 }
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: '服务器错误',
      data: null
    });
  }
});

module.exports = router;